package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.mapper.GsNavigationMapper;
import com.sdy.supermarket.biz.model.GsNavigation;
import com.sdy.supermarket.biz.model.GsRes;
import com.sdy.supermarket.biz.utils.TreeUtils;
import com.sdy.supermarket.biz.vo.GsNavigationVo;
import com.sdy.supermarket.biz.service.GsNavigationService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hyh
 * @since 2019-07-24
 */
@Slf4j
@Service
public class GsNavigationServiceImpl extends BaseServiceImpl<GsNavigation> implements GsNavigationService {
    @Resource
    private GsNavigationMapper navigationMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteNavigation(Integer id) throws BizException {
        GsNavigation byId = navigationMapper.selectById(id);
        Assert.isNull(byId, "目标为NULL");
        List<GsNavigation> list = navigationMapper.selectList(Wrappers.<GsNavigation>lambdaQuery().gt(GsNavigation::getSort, byId.getSort()).orderByAsc(GsNavigation::getSort));
        int deleteById = navigationMapper.deleteById(id);
        if (list.size() == 0) {
            return deleteById;
        }
        list.forEach(gs -> gs.setSort(gs.getSort() - 1));
        int batchUpdate = navigationMapper.batchUpdate(list);
        return batchUpdate;
    }

    @Override
    public List<GsNavigation> queryAll() {
        List<GsNavigation> gsNavigations = navigationMapper.selectList(Wrappers.<GsNavigation>lambdaQuery().orderByAsc(GsNavigation::getSort));
        return gsNavigations;
    }

    /**
     * 等级提升&降低
     * 首先根据ID查询当前拥有相同父节点的相邻节点的数据
     * 然后将自身节点等级替换为目标节点等级，之后将目标节点等级降低或下降
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int levelUpOrDown(Integer id, Integer type) throws BizException {
        GsNavigation byId = navigationMapper.selectById(id);
        Assert.isNull(byId, "查询不到数据");
        LambdaQueryWrapper<GsNavigation> query = new LambdaQueryWrapper<>();
        if (type == 0) {
            query.orderByDesc(GsNavigation::getSort).lt(GsNavigation::getSort, byId.getSort());
        }
        if (type == 1) {
            query.orderByAsc(GsNavigation::getSort).gt(GsNavigation::getSort, byId.getSort());
        }
        List<GsNavigation> navigationList = navigationMapper.selectList(query);
        if (navigationList.size() != 0 && navigationList.get(0) != null) {
            GsNavigation up = new GsNavigation().setId(id).setSort(navigationList.get(0).getSort());
            GsNavigation down = new GsNavigation().setId(navigationList.get(0).getId()).setSort(byId.getSort());
            boolean upStatus = updateById(up);
            boolean downStatus = updateById(down);
            if (!upStatus && !downStatus) {
                log.error("修改排序失败");
                throw new BizException("修改失败");
            }
            return 1;
        }
        Assert.isTrue(true, "排序等级已经最高或最低");
        return 0;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int saveOrUpdateNavigation(GsNavigation gsNavigation) throws BizException {
        if (gsNavigation.getId() == null) {
            List<GsNavigation> list = navigationMapper.selectList(new LambdaQueryWrapper<>());
            Assert.isTrue(list.size() == 10, "当前导航数量已达最大值");
            gsNavigation.setSort(list.size() + 1).setCreateTime(new Date());
            if (gsNavigation.getOpenType() == null) gsNavigation.setOpenType(0);
            if (gsNavigation.getDisplayStatus() == null) gsNavigation.setOpenType(0);
            if (gsNavigation.getFloatShow() == null) gsNavigation.setOpenType(0);
        }
        if (gsNavigation.getId() != null) {
            gsNavigation.setSort(null).setCreateTime(null);
        }
        boolean saveOrUpdate = saveOrUpdate(gsNavigation);
        return saveOrUpdate ? 1 : 0;
    }

}
